package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
/**
 * 自定义列表内容显示适配器
 * @author lhy
 * 2015/11
 */
public class MultipleListAdapter extends CommonAdapter<StringBuffer>{
	public MultipleListAdapter(Context mContext) {
		super(mContext);
	}
	@Override
	public void convert(ViewHolder holder, StringBuffer t) {
		LinearLayout layout = holder.getView(R.id.add_textview_layout);
		layout.removeAllViews();
		String [] textarr = t.toString().split(";");
//		initchance(textarr, layout);
		initmulti(textarr, layout);
	}
	public void initmulti(String [] textarr,LinearLayout layout){
		layout.setOrientation(LinearLayout.HORIZONTAL);
		TextView [] textitem = new TextView[textarr.length];
		TextView [] textitem2 = new TextView[textarr.length];
		LinearLayout linear1 = new LinearLayout(mContext);
		linear1.setId(01);
		linear1.setOrientation(LinearLayout.VERTICAL);
		linear1.setGravity(Gravity.RIGHT);
		LinearLayout linear2 = new LinearLayout(mContext);
		linear2.setId(02);
		linear2.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams linparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		layout.addView(linear1, linparams);
		layout.addView(linear2, linparams);
		for (int i = 0; i < textarr.length-1; i++) {
			String [] tex = textarr[i].split("/:");
			textitem[i] = new TextView(mContext);
			textitem[i].setMaxWidth(AppContext.screenW*1/2);
			textitem[i].setId(1000+i);
			textitem[i].setText(tex[0]+" ： ");
			textitem[i].setTextColor(Color.GRAY);
			textitem[i].setTextSize(16);
			textitem[i].setSingleLine();
			textitem[i].setEllipsize(TruncateAt.START);
			textitem2[i] = new TextView(mContext);
			textitem2[i].setId(100+i);
			if(tex.length==1){
				textitem2[i].setText("");
			}else{
				textitem2[i].setText(tex[1]);
			}
			textitem2[i].setTextColor(Color.BLACK);
			textitem2[i].setTextSize(16);
			textitem2[i].setSingleLine();
			textitem2[i].setEllipsize(TruncateAt.valueOf("END"));
			LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			linear1.addView(textitem[i], params);
			linear2.addView(textitem2[i], params2);
			
		}
	}
	public void initchance(String [] textarr,LinearLayout layout){
		TextView [] textitem = new TextView[textarr.length];
		TextView [] textitem2 = new TextView[textarr.length];
		LinearLayout [] linear = new LinearLayout[textarr.length];
		for (int i = 0; i < textarr.length-1; i++) {
			linear[i] = new LinearLayout(mContext);
			linear[i].setId(10000+i);
			linear[i].setWeightSum(5);
			linear[i].setOrientation(LinearLayout.HORIZONTAL);
			String [] tex = textarr[i].split("/:");
			textitem[i] = new TextView(mContext);
			textitem[i].setId(1000+i);
			textitem[i].setText(tex[0]+" ： ");
			textitem[i].setTextColor(Color.GRAY);
			textitem[i].setTextSize(18);
			textitem[i].setGravity(Gravity.RIGHT);
			textitem[i].setSingleLine();
			textitem[i].setEllipsize(TruncateAt.START);
			textitem2[i] = new TextView(mContext);
			textitem2[i].setId(100+i);
			if(tex.length==1){
				textitem2[i].setText("");
			}else{
				textitem2[i].setText(tex[1]);
			}
			textitem2[i].setTextColor(Color.BLACK);
			textitem2[i].setTextSize(16);
			textitem2[i].setSingleLine();
			textitem2[i].setEllipsize(TruncateAt.END);
			LinearLayout.LayoutParams linparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,2);
			LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,3);
			linear[i].addView(textitem[i], params2);
			linear[i].addView(textitem2[i], params);
			layout.addView(linear[i],linparams);
		}
	}
	@Override
	public int getLayoutId() {
		return R.layout.multiple_list_layout;
	}

}
